package com.bright.tactiviti.activiti.controller;

import com.bright.tactiviti.activiti.entity.vo.ProcessNodeVo;
import com.bright.tactiviti.activiti.service.ActCategoryService;
import com.bright.tactiviti.activiti.service.ActModelService;
import com.bright.tactiviti.activiti.service.ActNodeService;
import com.bright.tactiviti.activiti.service.ActProcessService;
import com.bright.tactiviti.common.constant.ActivitiConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.activiti.bpmn.model.*;
import org.activiti.bpmn.model.Process;
import org.activiti.engine.RepositoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


@Slf4j
@RestController
@Api("流程定义管理接口")
@RequestMapping("/tactiviti/actProcess")
@Transactional
public class ActProcessController {
    @Autowired
    private ActModelService actModelService;

    @Autowired
    private ActProcessService processService;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private ActNodeService actNodeService;

    @Autowired
    private ActCategoryService actCategoryService;

    @GetMapping("/getProcessNode/{id}")
    @ApiOperation("通过流程定义id获取流程节点")
    public List<ProcessNodeVo> getProcessNode(@ApiParam("流程定义id") @PathVariable String id){
        BpmnModel bpmnModel = repositoryService.getBpmnModel(id);

        List<ProcessNodeVo> list = new ArrayList<>();

        List<Process> processes = bpmnModel.getProcesses();
        if(processes==null || processes.size()==0){
            return null;
        }
        processes.forEach(process -> {
            Collection<FlowElement> elements = process.getFlowElements();
            elements.forEach(flowElement -> {
                ProcessNodeVo nodeVo = new ProcessNodeVo();
                nodeVo.setId(flowElement.getId());
                nodeVo.setTitle(flowElement.getName());
                if(flowElement instanceof StartEvent){
                    nodeVo.setType(ActivitiConstant.NODE_TYPE_START);
                }else if(flowElement instanceof UserTask){
                    //用户任务
                    nodeVo.setType(ActivitiConstant.NODE_TYPE_TASK);
                }else if(flowElement instanceof EndEvent){
                    //结束
                    nodeVo.setType(ActivitiConstant.NODE_TYPE_END);
                }else {
                    //排除其他连接线或节点

                }
                list.add(nodeVo);
            });
        });
        return list;
    }
}
